<template>
  <div>
    <p slot="title" style="text-align:center;">请您选择需要进入的单位或部门</p>
    <div class="countBox" v-for="(user,index) in userList" :key="user.userId">
      <div>
        <div class="firstChild">
          <p class="gardens"></p>
          <p>
            <a-badge :count="user.undoCount">
              <span class="fullDeptName">{{ user.fullDeptName }}</span>
            </a-badge>
          </p>
        </div>
        <div>
          <a-button type="primary" class="goTypeBtn" @click="login(user)" :ghost="true">点击进入</a-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>

import {afterCheckLogin} from "../../api/login";
import storage from '@/utils/storage'

export default {
  name: 'CompanyType',
  props: {
    userList: {
      type: Array,
      required: true
    },
    loginData: {
      required: true
    },
    pwd: {
      type: String,
      required: true
    },
    // 登陆方式，false为账号密码，true为短信验证码
    loginWay: {
      type: Boolean,
      required: false,
      default: false
    },
    // 当登陆方式为true时的请求凭证
    flag: {
      type: String,
      required: false,
      default: '123456'
    },
  },
  methods: {
    login(record) {
      this.loginData.deptId = record.deptId;
      this.loginData.tenantId = record.tenantId;
      this.loginData.userCode = record.userCode;
      if (this.loginWay) {
        this.loginData.flag = this.flag
      }
      afterCheckLogin(this.loginData).then(response => {
        if (response.code === 0) {
          this.loginHandle(response.data);
        } else {
          let data = response.data;
          let changePwd = data.changePwd;
          if (changePwd) {
            let that = this;
            this.$warning({
              content: response.message,
              onOk() {
                storage.session.set("mobile", data.userCode);
                that.$router.push({path: "/resetPassword"});
              },
              okText: '确认'
            });
          } else {
            this.$message.error(response.message);
            this.$emit('failed')
          }
        }
      }).catch(() => {
        this.$emit('failed')
      })
    },
    loginHandle(data) {
      console.log(data)
      storage.session.set('authToken', data.authToken)
      storage.session.set('userInfo', data)
      storage.session.set('isLogin', true)
      this.$router.push('/portal')
    },
  }
}
</script>
<style scoped>
.countBox {
  width: 100%;
  margin: 10px 0;
  box-sizing: border-box;
}

.countBox > div {
  display: flex;
  justify-content: space-between;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.countBox .firstChild {
  display: flex;
  width: 356px;
  line-height: 32px;
}

.countBox .firstChild .fullDeptName {
  padding-right: 15px;
  display: inline-block;
  width: 322px;
}

.countBox .gardens {
  width: 16px;
  height: 16px;
  background: red;
  border-radius: 50%;
  margin: 8px 10px 0 0;
}

.countBox .goTypeBtn {
  float: right;
}
</style>


